package com.yyplatform.admin.platform.generator.controller;

import static com.yyplatform.admin.Constants.URL_PREFIX;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yyplatform.admin.infra.model.RestResponse;
import com.yyplatform.admin.platform.generator.model.dto.TableConfigDTO;
import com.yyplatform.admin.platform.generator.model.dto.TableConfigFormDTO;
import com.yyplatform.admin.platform.generator.model.dto.TableConfigQueryDTO;
import com.yyplatform.admin.platform.generator.service.TableConfigService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author sleepforests@163.com
 * <p>
 * Created on 2024/9/22 21:10
 */
@Slf4j
@Tag(name = "开发/表配置")
@AllArgsConstructor
@RestController
@RequestMapping(URL_PREFIX + "/generator/tableConfig/")
public class TableConfigController {

    private TableConfigService tableConfigService;


    @GetMapping("query")
    @Operation(summary = "查询")
    @RequiresPermissions("sys:tableConfig:query")
    public RestResponse<Page<TableConfigDTO>> page(TableConfigQueryDTO pageQueryDTO) {
        log.debug("query dto is {}", pageQueryDTO);

        Page<TableConfigDTO> page = tableConfigService.query(pageQueryDTO);

        return RestResponse.ok(page);
    }


    @GetMapping("get")
    @Operation(summary = "通过ID获取详情")
    @RequiresPermissions("sys:tableConfig:get")
    public RestResponse<TableConfigDTO> get(Long id) {
        log.debug("get by id is {}", id);

        TableConfigDTO data = tableConfigService.get(id);
        return RestResponse.ok(data);
    }

    @GetMapping("init")
    @Operation(summary = "初始化table基本信息")
    @RequiresPermissions("sys:tableConfig:init")
    public RestResponse<TableConfigDTO> init() {
        tableConfigService.initAllTable();
        return RestResponse.ok();
    }


    @PostMapping("reset")
    @Operation(summary = "重置table和column数据")
    @RequiresPermissions("sys:tableConfig:reset")
    public RestResponse<TableConfigDTO> reset(Long id) {
        log.debug("reset by id is {}", id);
        tableConfigService.resetAll(id);
        return RestResponse.ok();
    }


    @PostMapping("save")
    @Operation(summary = "保存")
    @RequiresPermissions("sys:tableConfig:save")
    public RestResponse<Void> save(@RequestBody TableConfigFormDTO formDTO) {
        log.debug("save dto is {}", formDTO);
        tableConfigService.save(formDTO);

        return RestResponse.ok(null);
    }


    @PostMapping("update")
    @Operation(summary = "修改")
    @RequiresPermissions("sys:tableConfig:update")
    public RestResponse<Void> update(@RequestBody TableConfigFormDTO formDTO) {
        log.debug("update dto is {}", formDTO);

        tableConfigService.update(formDTO);
        return RestResponse.ok(null);
    }


    @PostMapping("delete")
    @Operation(summary = "删除")
    @RequiresPermissions("sys:tableConfig:delete")
    public RestResponse<Void> delete(Long id) {
        log.debug("delete by id is {}", id);

        tableConfigService.delete(id);
        return RestResponse.ok(null);
    }

}
